//
//  StaffTimeChooseView.m
//  QRMaster
//
//  Created by myApple on 16/5/20.
//  Copyright © 2016年 myApple. All rights reserved.
//

#import "StaffTimeChooseView.h"
#import "setTimeModel.h"
#import "staffTimeCell.h"

@interface StaffTimeChooseView()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView * staffTiemTabelVeiw;
    NSArray * timeArray;
    NSArray * weekData;
    NSMutableArray * selectTimeArray;
}

@end

@implementation StaffTimeChooseView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        selectTimeArray = [[NSMutableArray alloc]init];
         weekData = @[NSLocalizedString(@"Sunday", nil),NSLocalizedString(@"Monday", nil),NSLocalizedString(@"Tuesday", nil),NSLocalizedString(@"Wednesday", nil),NSLocalizedString(@"Thursday", nil),NSLocalizedString(@"Friday", nil),NSLocalizedString(@"Saturday", nil)];
        [self prepareTabelView];
    }
    return self;
}

-(void)setTotalTimeArray:(NSArray *)totalTimeArray
{
    _totalTimeArray = totalTimeArray;
    timeArray = totalTimeArray;
}

-(void)prepareTabelView
{
    float x = 0;
    float y = 0.5;
    float width = self.frame.size.width;
    float height = self.frame.size.height;
    staffTiemTabelVeiw = [[UITableView alloc]initWithFrame:CGRectMake(x, y, width, height) style:UITableViewStylePlain];
    staffTiemTabelVeiw.delegate = self;
    staffTiemTabelVeiw.dataSource = self;
    staffTiemTabelVeiw.tableFooterView = [UIView new];
    [self addSubview:staffTiemTabelVeiw];
    
    y = 0;
    height = 0.5;
    UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    lineView.backgroundColor = UIColorFromRGB(0xc1c1c1);
    [self addSubview:lineView];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return timeArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 56.0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * string = @"cell";
    staffTimeCell * cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (!cell) {
        cell = [[staffTimeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
        cell.backgroundColor = UIColorFromRGB(0xf5f5f5);
    }
    setTimeModel * model = timeArray[indexPath.row];
    cell.beginTime = model.beginTime;
    cell.endTime = model.endTime;
    cell.weekInfo = model.weekInfo;
    cell.timeId = model.timeId;
    cell.timeImage = model.timeImage;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    staffTimeCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark ) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [selectTimeArray removeObject:cell.timeId];
    }
    else
    {
        [selectTimeArray addObject:cell.timeId];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    if (_delegate && [_delegate respondsToSelector:@selector(selectTimeId:)]) {
        [_delegate selectTimeId:[self changeArrayToString:selectTimeArray]];
    }
}

#pragma mark --------- 将数组变成字符串
-(NSString *)changeArrayToString:(NSArray *)array
{
    NSMutableString * roomIdString = [[NSMutableString alloc]init];
    if (array.count > 0) {
        for (NSString * selectRoomid in array) {
            [roomIdString appendString:selectRoomid];
            [roomIdString appendString:@","];
        }
        [roomIdString substringToIndex:[roomIdString length]-2];
        @try {
            NSRange roomRange;
            roomRange.location = roomIdString.length - 1;
            roomRange.length = 1;
            [roomIdString deleteCharactersInRange:roomRange];
        }
        @catch (NSException *exception) {
            return nil;
        }
    }
    return roomIdString;
   
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
